<?php
namespace App\Http\Controllers\Api;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Models\News;
class NewsController extends Controller
{
	/**
	 * 首页推荐
	 */
    public function index()
    {
    	return News::where('is_recommend',1)->get();
    }

    /**
     * 新闻列表
     */
    public function getList()
    {
    	return News::get();
    }

    /**
     * 新闻详情
     */
    public function show($id)
    {
    	$row = News::findOrFail($id);
    	return $row;
    }

    /**
     * 抓取数据
     * @param Request $request
     */
    function captureData(Request $request){
        $fh = fopen('https://www.panda.tv/cate/yzdr', 'r');
        if($fh){
            while(!feof($fh)) {
                echo fgets($fh);
            }
        }
    }

    /**
     * 查询段子返回json
     * @return string
     */
    function getNewJsonData(){
        $data['news'] =$this->news->where('type','=',1)
            ->orderBy('created_at', 'src')
            ->take(5)
            ->get()->toArray();
        foreach ($data['news'] as $key=>$value){
            $data['news'][$key]['avatar'] = image_file_exists($value['avatar']);
        }
        return json_encode($data);
    }
}
